import subprocess
import argparse
import os

def generate_diff_flamegraph(file1, file2, diff_file, svg_file):
    # Step 1: Generate the diff folded stack trace
    diff_cmd = f"./FlameGraph/difffolded.pl {file1} {file2} > {diff_file}"
    print(f"Generating diff folded stack trace: {diff_cmd}")
    subprocess.run(diff_cmd, shell=True, check=True)
    
    # Step 2: Generate the diff flame graph
    flamegraph_cmd = f"./FlameGraph/flamegraph.pl {diff_file} > {svg_file}"
    print(f"Generating diff flame graph: {flamegraph_cmd}")
    subprocess.run(flamegraph_cmd, shell=True, check=True)
    
    print(f"Diff flame graph saved as {svg_file}")

def main():
    parser = argparse.ArgumentParser(description="Generate a diff flame graph using two folded files.")
    parser.add_argument("file1", help="First folded file.")
    parser.add_argument("file2", help="Second folded file.")
    parser.add_argument("-d", "--diff-file", default="diff.folded", help="Output diff folded file name.")
    parser.add_argument("-s", "--svg-file", default="diff.svg", help="Output SVG file name for diff flame graph.")

    args = parser.parse_args()
    generate_diff_flamegraph(args.file1, args.file2, args.diff_file, args.svg_file)

if __name__ == "__main__":
    main()
